package modules.battle.bottom
{
	import core.Language;
	import core.manager.battle.BattleManager;
	import core.manager.battle.events.BattleEvent;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import framework.core.TipManager;
	import framework.proxy.ComponentProxy;
	import framework.view.components.bar.ProgressBarProxy;
	
	import mapengine.avatars.IGameAvatar;
	
	import mx.utils.StringUtil;
	
	import values.avatar.AvatarTemplateValueList;
	import values.events.AvatarValueEvent;
	
	import view.component.asset.IconProxy;
	
	public class BattleAvatarDetailProxy extends ComponentProxy
	{
		public function BattleAvatarDetailProxy()
		{
			super();
		}
		
		override protected function get proxyMapping():Array
		{
			return [
				{skin:"levelConstTF" , text:Language.getText("@level_label")},
				{skin:"hpConstTF" , text:Language.getText("@property_maxHP_name")},
				{skin:"moveConstTF" , text:Language.getText("@property_moveCapability_name")},
				{skin:"attackConstTF" , text:Language.getText("@property_attact_distance_name")},
				{skin:"mpConstTF" , text:Language.getText("@property_maxMP_name")},
				{skin:"mpBar" , classRef:ProgressBarProxy , id:"mpBar"},
				{skin:"skillIcon" , classRef:IconProxy , id:"skillIcon"},
				{skin:"ultimateIcon" , classRef:IconProxy , id:"ultimateIcon"},
				{skin:"buffIcon" , classRef:IconProxy , id:"buffIcon"},
				{skin:"debuffIcon" , classRef:IconProxy , id:"debuffIcon"}
			];
		}
		
		protected function get mpBar():ProgressBarProxy
		{
			return proxys["mpBar"];
		}
		
		protected function get skillIcon():IconProxy
		{
			return proxys["skillIcon"];
		}
		
		protected function get ultimateIcon():IconProxy
		{
			return proxys["ultimateIcon"];
		}
		
		protected function get buffIcon():IconProxy
		{
			return proxys["buffIcon"];
		}
		
		protected function get debuffIcon():IconProxy
		{
			return proxys["debuffIcon"];
		}
		
		private var _vo:IGameAvatar;

		public function get vo():IGameAvatar
		{
			return _vo;
		}
		
		override public function parse(target:MovieClip):void
		{
			super.parse(target);
			mpBar.maxValue = AvatarTemplateValueList.maxMP;
			mpBar.minValue = 0;
		}

		public function set vo(value:IGameAvatar):void
		{
			removeEvents();
			_vo = value;
			addEvents();
			if(!vo)
			{
				skin.levelTF.text = "";
				skin.attackTF.text = "";
				skillIcon.stopAndRemove();
				ultimateIcon.stopAndRemove();
				mpBar.currentValue = 0;
				skin.hpTF.text = "";
				skin.moveTF.text = "";
				debuffIcon.stopAndRemove();
				debuffIcon.skinContent.removeEventListener(MouseEvent.MOUSE_OVER , onIconMouseOver);
				buffIcon.stopAndRemove();
				buffIcon.skinContent.removeEventListener(MouseEvent.MOUSE_OVER , onIconMouseOver);
				skin.ultimateRoundTF.text = "";
				skin.ultimateNumTF.text = "";
			}
			else
			{
				skin.levelTF.text = vo.avatarValue.level;
				skin.attackTF.text = vo.avatarValue.template.distance;
				if(vo.avatarValue.selectedSkill)
				{
					skillIcon.load(vo.avatarValue.selectedSkill.icon);
				}
				if(vo.avatarValue.ultimateSkill)
				{
					ultimateIcon.load(vo.avatarValue.ultimateSkill.icon);
				}
				onBuffChange(null);
				onBuffChange(null);
				onPropertyChange(null);
				onRoundChange(null);
				onUltimateUse(null);
			}
		}
		
		private function addEvents():void
		{
			if(!vo) return;
			vo.avatarValue.addEventListener(AvatarValueEvent.ADD_WAR_BUFF , onBuffChange);
			vo.avatarValue.addEventListener(AvatarValueEvent.ADD_WAR_DEBUFF , onDebuffChange);
			vo.avatarValue.addEventListener(AvatarValueEvent.PROPERTY_CHANGE , onPropertyChange);
			BattleManager.battleManager.addEventListener(BattleEvent.ROUND_CHANGE , onRoundChange);
			vo.avatarValue.addEventListener(AvatarValueEvent.USE_ULTIMATE , onUltimateUse);
			if(vo.avatarValue.selectedSkill) skillIcon.skinContent.addEventListener(MouseEvent.MOUSE_OVER , onIconMouseOver);
			if(vo.avatarValue.ultimateSkill) ultimateIcon.skinContent.addEventListener(MouseEvent.MOUSE_OVER , onIconMouseOver);
		}
		
		private function removeEvents():void
		{
			if(!vo) return;
			vo.avatarValue.removeEventListener(AvatarValueEvent.ADD_WAR_BUFF , onBuffChange);
			vo.avatarValue.removeEventListener(AvatarValueEvent.ADD_WAR_DEBUFF , onDebuffChange);
			vo.avatarValue.removeEventListener(AvatarValueEvent.PROPERTY_CHANGE , onPropertyChange);
			BattleManager.battleManager.removeEventListener(BattleEvent.ROUND_CHANGE , onRoundChange);
			vo.avatarValue.removeEventListener(AvatarValueEvent.USE_ULTIMATE , onUltimateUse);
			if(vo.avatarValue.selectedSkill) skillIcon.skinContent.removeEventListener(MouseEvent.MOUSE_OVER , onIconMouseOver);
			if(vo.avatarValue.ultimateSkill) ultimateIcon.skinContent.removeEventListener(MouseEvent.MOUSE_OVER , onIconMouseOver);
		}
		
		override public function dispose():void
		{
			buffIcon.skinContent.removeEventListener(MouseEvent.MOUSE_OVER , onIconMouseOver);
			debuffIcon.skinContent.removeEventListener(MouseEvent.MOUSE_OVER , onIconMouseOver);
			removeEvents();
			_vo = null;
			super.dispose();
		}
		
		private function onBuffChange(evt:Event):void
		{
			if(vo.avatarValue.buff)
			{
				buffIcon.load(vo.avatarValue.buff.icon);
				buffIcon.skinContent.addEventListener(MouseEvent.MOUSE_OVER , onIconMouseOver);
			}
			else
			{
				buffIcon.stopAndRemove();
				buffIcon.skinContent.removeEventListener(MouseEvent.MOUSE_OVER , onIconMouseOver);
			}
		}
		
		private function onDebuffChange(evt:Event):void
		{
			if(vo.avatarValue.debuff)
			{
				debuffIcon.load(vo.avatarValue.debuff.icon);
				debuffIcon.skinContent.addEventListener(MouseEvent.MOUSE_OVER , onIconMouseOver);
			}
			else
			{
				debuffIcon.stopAndRemove();
				debuffIcon.skinContent.removeEventListener(MouseEvent.MOUSE_OVER , onIconMouseOver);
			}
		}
		
		private function onPropertyChange(evt:Event):void
		{
			mpBar.currentValue = vo.avatarValue.mp;
			skin.hpTF.text = vo.avatarValue.hp;
			skin.moveTF.text = vo.avatarValue.moveCapability;
		}
		
		private function onRoundChange(evt:Event):void
		{
			var str:String;
			str = StringUtil.substitute(Language.getText("@ultimate_next_round") , vo.avatarValue.ultimateSkill.nextUseRound);
			skin.ultimateRoundTF.text = str;
		}
		
		private function onUltimateUse(evt:Event):void
		{
			skin.ultimateNumTF.text = StringUtil.substitute(Language.getText("@ultimate_left_count") , vo.avatarValue.ultimateSkill.useCount);
		}
		
		private function onIconMouseOver(evt:Event):void
		{
			switch(evt.target)
			{
				case buffIcon.skinContent:
					TipManager.showTip(buffIcon.skinContent , vo.avatarValue.buff.desc);
					break;
				case debuffIcon.skinContent:
					TipManager.showTip(debuffIcon.skinContent , vo.avatarValue.debuff.desc);
					break;
				case skillIcon.skinContent:
					TipManager.showTip(skillIcon.skinContent , vo.avatarValue.selectedSkill.template.desc);
					break;
				case ultimateIcon.skinContent:
					TipManager.showTip(ultimateIcon.skinContent , vo.avatarValue.ultimateSkill.template.desc);
					break;
				default:
					break;
			}
		}

	}
}